import { constantRoutes } from "@/router";
import { getMenu } from "@/api/login";
import Layout from "@/views/layout/Layout";

function filterAsyncRouter(asyncRouterMap) {
    // 遍历后台传来的路由字符串，转换为组件对象
    try {
        const accessedRouters = asyncRouterMap.filter(route => {
            if (route.component) {
                if (route.component === "Layout") {
                    // Layout组件特殊处理
                    route.component = Layout;
                } else {
                    const component = route.component;
                    route.component = resolve => {
                        require(["@/views" + component + ".vue"], resolve);
                    };
                }
            }
            if (route.children && route.children.length) {
                route.children = filterAsyncRouter(route.children);
            }
            return true;
        });
        return accessedRouters;
    } catch (e) {
        console.log(e);
    }
}

const state = {
    routes: [],
    addRoutes: []
};

const mutations = {
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes;
        state.routes = constantRoutes.concat(routes);
    }
};
const actions = {
    async generateRoutes({ commit }, roles) {
        // 取后台路由
        let list = [];
        console.log(
            "222",
            window.sessionStorage.getItem("userName"),
            window.sessionStorage.getItem("department")
        );
        if (window.sessionStorage.getItem("userName") == "admin") {
            list = [{
                    path: "/",
                    component: () =>
                        import ("@/views/layout/Layout"),
                    redirect: "/orderManagement",
                    name: "orderManagement",
                    children: [{
                        path: "orderManagement",
                        component: () =>
                            import ("@/views/orderManagement"),
                        meta: { title: "订单管理", icon: "el-icon-platform-eleme" }
                    }]
                },
                {
                    path: "/personnelManagement",
                    // component: () => import('@/views/personnelManagement/index'),
                    component: () =>
                        import ("@/views/layout/Layout"),
                    name: "personnelManagement",
                    meta: { title: "人员管理", icon: "chart" },
                    children: [{
                        path: "",
                        component: () =>
                            import ("@/views/personnelManagement/index"),
                        name: "personnelManagement",
                        meta: { title: "人员管理" }
                    }]
                },
                {
                    path: "/orderProductDetails",
                    // component: () => import('@/views/orderProductDetails/index'),
                    component: () =>
                        import ("@/views/layout/Layout"),
                    name: "产品管理",
                    meta: { title: "产品管理", icon: "chart" },
                    children: [{
                        path: "",
                        component: () =>
                            import ("@/views/orderProductDetails/index"),
                        name: "orderProductDetails",
                        meta: { title: "产品管理" }
                    }]
                },
                {
                    path: "/dispatchList",
                    // component: () => import('@/views/dispatchList/index'),
                    component: () =>
                        import ("@/views/layout/Layout"),
                    name: "派单",
                    meta: { title: "派单", icon: "chart" },
                    children: [{
                        path: "",
                        component: () =>
                            import ("@/views/dispatchList/index"),
                        name: "dispatchList",
                        meta: { title: "派单" }
                    }]
                },
                {
                    path: "/requisitionList",
                    // component: () => import('@/views/requisitionList/index'),
                    component: () =>
                        import ("@/views/layout/Layout"),
                    name: "请购清单",
                    meta: { title: "请购清单", icon: "chart" },
                    children: [{
                        path: "",
                        component: () =>
                            import ("@/views/requisitionList/index"),
                        name: "requisitionList",
                        meta: { title: "请购清单" }
                    }]
                },
                {
                    path: "/orderFee",
                    component: Layout,
                    name: "orderFee",
                    meta: { title: "订单费用", icon: "chart" },
                    children: [{
                        path: "",
                        component: () =>
                            import ("@/views/orderFee/index"),
                        name: "orderFee",
                        meta: { title: "订单费用" }
                    }]
                }
            ];
        } else {
            if (window.sessionStorage.getItem("department") == 2) {
                // 业务  订单和产品
                list = [{
                        path: "/",
                        component: () =>
                            import ("@/views/layout/Layout"),
                        redirect: "/orderManagement",
                        name: "orderManagement",
                        children: [{
                            path: "orderManagement",
                            component: () =>
                                import ("@/views/orderManagement"),
                            meta: { title: "订单管理", icon: "el-icon-platform-eleme" }
                        }]
                    },
                    {
                        path: "/orderProductDetails",
                        // component: () => import('@/views/orderProductDetails/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "产品管理",
                        meta: { title: "产品管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/orderProductDetails/index"),
                            name: "orderProductDetails",
                            meta: { title: "产品管理" }
                        }]
                    }
                ];
            } else if (window.sessionStorage.getItem("department") == 4) {
                // pm
                list = [{
                        path: "/",
                        component: () =>
                            import ("@/views/layout/Layout"),
                        redirect: "/orderManagement",
                        name: "orderManagement",
                        children: [{
                            path: "orderManagement",
                            component: () =>
                                import ("@/views/orderManagement"),
                            meta: { title: "订单管理", icon: "el-icon-platform-eleme" }
                        }]
                    },
                    {
                        path: "/personnelManagement",
                        // component: () => import('@/views/personnelManagement/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "personnelManagement",
                        meta: { title: "人员管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/personnelManagement/index"),
                            name: "personnelManagement",
                            meta: { title: "人员管理" }
                        }]
                    },
                    {
                        path: "/orderProductDetails",
                        // component: () => import('@/views/orderProductDetails/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "产品管理",
                        meta: { title: "产品管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/orderProductDetails/index"),
                            name: "orderProductDetails",
                            meta: { title: "产品管理" }
                        }]
                    },
                    {
                        path: "/dispatchList",
                        // component: () => import('@/views/dispatchList/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "派单",
                        meta: { title: "派单", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/dispatchList/index"),
                            name: "dispatchList",
                            meta: { title: "派单" }
                        }]
                    },
                    {
                        path: "/requisitionList",
                        // component: () => import('@/views/requisitionList/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "请购清单",
                        meta: { title: "请购清单", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/requisitionList/index"),
                            name: "requisitionList",
                            meta: { title: "请购清单" }
                        }]
                    }
                ];
            } else if (window.sessionStorage.getItem("department") == 7) {
                // 生产主管
                list = [{
                        path: "/",
                        component: () =>
                            import ("@/views/layout/Layout"),
                        redirect: "/orderManagement",
                        name: "orderManagement",
                        children: [{
                            path: "orderManagement",
                            component: () =>
                                import ("@/views/orderManagement"),
                            meta: { title: "订单管理", icon: "el-icon-platform-eleme" }
                        }]
                    },
                    {
                        path: "/orderProductDetails",
                        // component: () => import('@/views/orderProductDetails/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "产品管理",
                        meta: { title: "产品管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/orderProductDetails/index"),
                            name: "orderProductDetails",
                            meta: { title: "产品管理" }
                        }]
                    },
                    {
                        path: "/dispatchList",
                        // component: () => import('@/views/dispatchList/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "派单",
                        meta: { title: "派单", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/dispatchList/index"),
                            name: "dispatchList",
                            meta: { title: "派单" }
                        }]
                    },
                    {
                        path: "/requisitionList",
                        // component: () => import('@/views/requisitionList/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "请购清单",
                        meta: { title: "请购清单", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/requisitionList/index"),
                            name: "requisitionList",
                            meta: { title: "请购清单" }
                        }]
                    }
                ];
            } else if (window.sessionStorage.getItem("department") == 5) {
                // 外协
                list = [{
                        path: "/",
                        component: () =>
                            import ("@/views/layout/Layout"),
                        redirect: "/orderManagement",
                        name: "orderManagement",
                        children: [{
                            path: "orderManagement",
                            component: () =>
                                import ("@/views/orderManagement"),
                            meta: { title: "订单管理", icon: "el-icon-platform-eleme" }
                        }]
                    },
                    {
                        path: "/orderProductDetails",
                        // component: () => import('@/views/orderProductDetails/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "产品管理",
                        meta: { title: "产品管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/orderProductDetails/index"),
                            name: "orderProductDetails",
                            meta: { title: "产品管理" }
                        }]
                    }
                ];
            } else if (window.sessionStorage.getItem("department") == 8) {
                // 品检
                list = [{
                        path: "/",
                        component: () =>
                            import ("@/views/layout/Layout"),
                        redirect: "/orderManagement",
                        name: "orderManagement",
                        children: [{
                            path: "orderManagement",
                            component: () =>
                                import ("@/views/orderManagement"),
                            meta: { title: "订单管理", icon: "el-icon-platform-eleme" }
                        }]
                    },
                    {
                        path: "/orderProductDetails",
                        // component: () => import('@/views/orderProductDetails/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "产品管理",
                        meta: { title: "产品管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/orderProductDetails/index"),
                            name: "orderProductDetails",
                            meta: { title: "产品管理" }
                        }]
                    },
                    {
                        path: "/dispatchList",
                        // component: () => import('@/views/dispatchList/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "派单",
                        meta: { title: "派单", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/dispatchList/index"),
                            name: "dispatchList",
                            meta: { title: "派单" }
                        }]
                    }
                ];
            } else if (window.sessionStorage.getItem("department") == 10) {
                // 仓库
                list = [{
                        path: "/",
                        component: () =>
                            import ("@/views/layout/Layout"),
                        redirect: "/orderManagement",
                        name: "orderManagement",
                        children: [{
                            path: "orderManagement",
                            component: () =>
                                import ("@/views/orderManagement"),
                            meta: { title: "订单管理", icon: "el-icon-platform-eleme" }
                        }]
                    },
                    {
                        path: "/orderProductDetails",
                        // component: () => import('@/views/orderProductDetails/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "产品管理",
                        meta: { title: "产品管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/orderProductDetails/index"),
                            name: "orderProductDetails",
                            meta: { title: "产品管理" }
                        }]
                    },
                    {
                        path: "/requisitionList",
                        // component: () => import('@/views/requisitionList/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "请购清单",
                        meta: { title: "请购清单", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/requisitionList/index"),
                            name: "requisitionList",
                            meta: { title: "请购清单" }
                        }]
                    }
                ];
            } else if (window.sessionStorage.getItem("department") == 9) {
                // 财务
                list = [{
                    path: "/orderFee",
                    component: Layout,
                    name: "orderFee",
                    meta: { title: "订单费用", icon: "chart" },
                    children: [{
                        path: "",
                        component: () =>
                            import ("@/views/orderFee/index"),
                        name: "orderFee",
                        meta: { title: "订单费用" }
                    }]
                }];
            } else {
                list = [{
                        path: "/",
                        component: () =>
                            import ("@/views/layout/Layout"),
                        redirect: "/orderManagement",
                        name: "orderManagement",
                        children: [{
                            path: "orderManagement",
                            component: () =>
                                import ("@/views/orderManagement"),
                            meta: { title: "订单管理", icon: "el-icon-platform-eleme" }
                        }]
                    },
                    {
                        path: "/personnelManagement",
                        // component: () => import('@/views/personnelManagement/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "personnelManagement",
                        meta: { title: "人员管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/personnelManagement/index"),
                            name: "personnelManagement",
                            meta: { title: "人员管理" }
                        }]
                    },
                    {
                        path: "/orderProductDetails",
                        // component: () => import('@/views/orderProductDetails/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "产品管理",
                        meta: { title: "产品管理", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/orderProductDetails/index"),
                            name: "orderProductDetails",
                            meta: { title: "产品管理" }
                        }]
                    },
                    {
                        path: "/dispatchList",
                        // component: () => import('@/views/dispatchList/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "派单",
                        meta: { title: "派单", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/dispatchList/index"),
                            name: "dispatchList",
                            meta: { title: "派单" }
                        }]
                    },
                    {
                        path: "/requisitionList",
                        // component: () => import('@/views/requisitionList/index'),
                        component: () =>
                            import ("@/views/layout/Layout"),
                        name: "请购清单",
                        meta: { title: "请购清单", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/requisitionList/index"),
                            name: "requisitionList",
                            meta: { title: "请购清单" }
                        }]
                    },
                    {
                        path: "/orderFee",
                        component: Layout,
                        name: "orderFee",
                        meta: { title: "订单费用", icon: "chart" },
                        children: [{
                            path: "",
                            component: () =>
                                import ("@/views/orderFee/index"),
                            name: "orderFee",
                            meta: { title: "订单费用" }
                        }]
                    }
                ];
            }
        }
        // console.log(list)
        // return new Promise(resolve => {
        //   const tmp = asyncRouter.data.permissionList
        //   const accessedRoutes = filterAsyncRouter(tmp)
        const accessedRoutes = list;
        commit("SET_ROUTES", accessedRoutes);
        // eslint-disable-next-line no-undef
        resolve(accessedRoutes);
        // })
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};